{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2de2969a",
   "metadata": {},
   "source": [
    "------\n",
    "* 学习人： 周晓源\n",
    "* 时间：2021年11月29日\n",
    "* week14\n",
    "* 学习线上资源：[python官方文档](https://docs.python.org)\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6c4ce25",
   "metadata": {},
   "source": [
    "# 学习目标：\n",
    "> 1. 函数的定义\n",
    "> 2. 函数的使用\n",
    "> 3. 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84ac6e5f",
   "metadata": {},
   "source": [
    "## 函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "58ec401e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T06:47:44.910616Z",
     "start_time": "2021-11-29T06:47:44.886995Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a, end=' ')\n",
    "    a, b = b, a+b\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "11a6fe7c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T06:52:36.384193Z",
     "start_time": "2021-11-29T06:52:36.376176Z"
    }
   },
   "outputs": [],
   "source": [
    "# ..........想重复使用某一个功能（避免重复造轮子）\n",
    "# ..........使用的人可以不懂代码，只要会调用函数即可function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f863a24",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 1. 定义函数的一般方法：\n",
    "...\n",
    "def function_name():# .function_name 遵循变量定义的规则  区别数学函数==> f(x)  python的函数可以自定义名称\n",
    "    \"\"\"函数的注释\"\"\"# 函数的注释  可以和使用者交流\n",
    "    函数体          # 函数体要和定义函数保持一定的tab缩进\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "69a06a37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T06:54:25.351646Z",
     "start_time": "2021-11-29T06:54:25.327600Z"
    }
   },
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to \n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fff37164",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T06:54:36.719377Z",
     "start_time": "2021-11-29T06:54:36.711790Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 \n"
     ]
    }
   ],
   "source": [
    "fib(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8085d17",
   "metadata": {},
   "source": [
    "## 练习：1.请大家写一个函数，实现hello world "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "30b2cf38",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:09:04.234469Z",
     "start_time": "2021-11-29T07:09:04.225291Z"
    }
   },
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印你好世界\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d9cbc40f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:09:12.348494Z",
     "start_time": "2021-11-29T07:09:12.340874Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58583716",
   "metadata": {},
   "source": [
    "## 练习：2.请大家写一个函数 实现hello username"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0c8181c9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:27:24.660871Z",
     "start_time": "2021-11-29T07:27:24.651306Z"
    }
   },
   "outputs": [],
   "source": [
    "def username(yy):    # write Fibonacci series up to \n",
    "    \"\"\"打印，你好xxx同学.\"\"\"\n",
    "    \n",
    "#     print(\"hello\",n)\n",
    "#修改：\n",
    "    print(\"hello, Mr.\"+ yy.title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7c1f49fd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:27:26.852855Z",
     "start_time": "2021-11-29T07:27:26.844851Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Mr.Ww\n"
     ]
    }
   ],
   "source": [
    "username(\"ww\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bea37fdc",
   "metadata": {},
   "source": [
    "### 函数的参数\n",
    "\n",
    "> 1. 形式参数:\n",
    ">> * 定义函数式，写在 function_name 括号里面的参数\n",
    ">> * 在定义函数式，形式参数不占用空间\n",
    "> 2. 实际参数: \n",
    ">> * 调用函数式，写在 function_name 括号里面的参数\n",
    ">> * 在使用函数时，形式参数被分配空间，变为实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "369b8c84",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:36:19.137743Z",
     "start_time": "2021-11-29T08:36:19.128432Z"
    }
   },
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):    \n",
    "    \"\"\"\n",
    "    显示宠物信息\n",
    "    通过打印的方式显示宠物类型和宠物名称\n",
    "    参数信息：\n",
    "    pet_name:是一个字符串，宠物名称\n",
    "    animal_type:是一个字符串，宠物类型\n",
    "    \"\"\"\n",
    "    print(\"\\nI have a \"+ animal_type+\".\")\n",
    "    print(\"my \"+ animal_type +\"'s name is \"+pet_name.title() +\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f40bc123",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:31:22.853959Z",
     "start_time": "2021-11-29T07:31:22.845956Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 \n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"cat\",\"喵喵\")# \"cat\",\"喵喵\"是实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c9e97a1c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:36:37.509723Z",
     "start_time": "2021-11-29T08:36:37.501710Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(animal_type, pet_name)\n",
      "    显示宠物信息\n",
      "    通过打印的方式显示宠物类型和宠物名称\n",
      "    参数信息：\n",
      "    pet_name:是一个字符串，宠物名称\n",
      "    animal_type:是一个字符串，宠物类型\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953202d9",
   "metadata": {},
   "source": [
    "### 当函数具有多个参数时\n",
    "> 1. 位置参数\n",
    ">> 1. 形式参数有先后顺序，函数调用过程中应该考虑参数先后的顺序，避免出现顺序错误的笑话\n",
    ">> 2.调用函数过程中要考虑形式参数的个数 \n",
    "> 2. 关键字参数\n",
    ">> 1. 调用函数时，通过赋值的方式传递给定义好的形参变量数据\n",
    ">> 2. 关键字参数不考虑先后顺序，因为是指定变量赋值\n",
    "> 3. 默认参数\n",
    ">> 1. 在形式参数中，提前预先设置一个用户可能使用的参数值\n",
    ">> 2. 用户在使用/调用函数时，可以省略不给默认参数赋值\n",
    ">> 3. 用户给默认参数赋值，将替换默认参数的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5b820737",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:46:09.369388Z",
     "start_time": "2021-11-29T07:46:09.361377Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "my cat's name is 喵喵.\n"
     ]
    }
   ],
   "source": [
    "# 在使用 describe_pet() 函数时 需要考虑参数的位置，从而尽心参数数据的输入（实参的调用）\n",
    "# 位置参数\n",
    "describe_pet(\"cat\",\"喵喵\")\n",
    "# 要根据 形式参数的位置先后顺序 先写animal_type,然后写pet_name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "49b09d0e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:46:07.481037Z",
     "start_time": "2021-11-29T07:46:07.457027Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "my cat's name is 喵喵.\n"
     ]
    }
   ],
   "source": [
    "#关键字参数使用\n",
    "describe_pet(pet_name=\"喵喵\",animal_type=\"cat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ce81b1e6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:48:58.601157Z",
     "start_time": "2021-11-29T07:48:58.592442Z"
    }
   },
   "outputs": [],
   "source": [
    "# 默认参数\n",
    "def describe_pet(animal_type,pet_name):    \n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \"+ animal_type+\".\")\n",
    "    print(\"my \"+ animal_type +\"'s name is \"+pet_name.title() +\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "963fd979",
   "metadata": {},
   "source": [
    "-----\n",
    "### 参数使用过程中的思考与问题\n",
    "> 1. 思考：关键字参数 和 位置参数，是否可以一起使用？（可以）\n",
    ">> 1. 报错：positional argument follows keyword argument  （位置参数<font style=\"color:red\">均</font>要放在关键字参数前面）\n",
    ">>> 1. 先位置参数，再是关键参数\n",
    ">> 2. 报错：test1() got multiple values for argument 'x'（映射，不能出现 multiple values（重复值））\n",
    "> 2. 定义函数：默认参数和非默认参数的先后关系\n",
    ">> 1.non-default argument follows default argument(非默认参数<font style=\"color:red\">均</font>要放在默认参数前面)\n",
    "-----\n",
    "* 以下为测试代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "fb62664f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:14:59.006883Z",
     "start_time": "2021-11-29T08:14:58.998882Z"
    }
   },
   "outputs": [],
   "source": [
    "def test1(x,y,z):\n",
    "    \"\"\"打印x，y，z的值\n",
    "    \"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0c2f01e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:20:21.038440Z",
     "start_time": "2021-11-29T08:20:21.028951Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "test1(1,2,z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "db7be513",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:23:38.971409Z",
     "start_time": "2021-11-29T08:23:38.947381Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "test1() got multiple values for argument 'x'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHOUXI~1\\AppData\\Local\\Temp/ipykernel_7828/2816279326.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtest1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: test1() got multiple values for argument 'x'"
     ]
    }
   ],
   "source": [
    "test1(1,x=2,z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "67614db1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:29:04.275686Z",
     "start_time": "2021-11-29T08:29:04.251636Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (Temp/ipykernel_7828/4106486920.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\ZHOUXI~1\\AppData\\Local\\Temp/ipykernel_7828/4106486920.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def test1(x,y=2,z):\u001b[0m\n\u001b[1;37m                     ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "def test1(x,y=2,z):\n",
    "    \"\"\"打印x，y，z的值\n",
    "    \"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e460b57",
   "metadata": {},
   "source": [
    "## 作业：课本练习实践\n",
    "> * 总结函数优势\n",
    "> * 预习课本第四章\n",
    ">> * 完成课本元音字母实践\n",
    ">> * 浏览模块，创建模块和调用模块，了解PEP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "739b3762",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:39:57.862415Z",
     "start_time": "2021-11-29T08:39:57.846998Z"
    }
   },
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"\n",
    "    \n",
    "    \"\"\"\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3bc889a7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:40:12.532536Z",
     "start_time": "2021-11-29T08:40:12.516792Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"2wfbsjkfkdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7605e6aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:40:44.314203Z",
     "start_time": "2021-11-29T08:40:44.290668Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"sdjfaaaaa\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00e553c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
